package com.edu.arrayList;

import java.util.ArrayList;
import java.util.Scanner;

public class arrayListDemo02 {
    //操作集合对象，实现查询学生信息操作
    public static void main(String[] args) {
        //创建一个学生集合用于存储学生对象
        ArrayList<student> students = new ArrayList<>();
        students.add(new student("1234","小明",20,"工业一班"));
        students.add(new student("12345","小王",33,"工业二班"));
        students.add(new student("12346","小张",44,"工业三班"));
        students.add(new student("12347","小红",55,"工业四班"));
        students.add(new student("12348","小梁",66,"工业五班"));
        System.out.println(students);//打印一下
        Scanner sn = new Scanner(System.in);
        while(true){//定义死循环，无限查询
            System.out.println("请输入你要查询的学生学号：");
            String num = sn.next();
            student ok=getStudentById(students,num);
            if(ok==null){
                System.out.println("查无此人");
            }else{
                System.out.println("学号\t\t"+"姓名\t\t"+"年龄\t\t"+"班级");
                System.out.print(ok.getStudentNum()+"\t\t");
                System.out.print(ok.getName()+"\t\t");
                System.out.print(ok.getAge()+"\t\t");
                System.out.println(ok.getClassName());
            }
        }
    }
    //编写通过学号查询的方法
    public static student getStudentById(ArrayList<student> stu,String Num){
        for (int i = 0; i < stu.size(); i++) {
            student s=stu.get(i);
            if(s.getStudentNum().equals(Num)){
                return s;//通过学号查找到学生的信息
            }
        }
        return null;//查无此人
    }
}
